package com.tydic.oa.mixture;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class FillData {
	private String sConnStr = "";
	private String sDBDriver = "oracle.jdbc.OracleDriver";
	/**
	* ????????
	*/
	public FillData() {
		sConnStr = "jdbc:oracle:thin:@HAWK:1521:orcl";
//		sConnStr = "jdbc:oracle:thin:@10.42.2.191:1521:lp3db1";
	}
	/**
	* @param ip,serviceName
	*/
	public FillData(String ip,String serviceName) {
		sConnStr = "jdbc:oracle:thin:@"+ip+":1521:"+serviceName;
	}
	/**
	* ???thin??????Oracle?????????.
	*/
	public Connection connectDbByThin() {
		Connection conn=null;
		try {
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr,"emc","emc");
		} catch (Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) {
		FillData toi = new FillData();
		Connection conn = toi.connectDbByThin();
	
		try {
			conn.setAutoCommit(true);
			
			Statement stmt = null;
			
			stmt = conn.createStatement();
			
			//?????SQL???
			StringBuffer sBuffer = new StringBuffer("select rt.rcid, rc.DEPTNAME ");
			sBuffer.append("from TBLBIZ_RC_TABLE rt, TBLBIZ_RC rc ");
			sBuffer.append("where RT.RCID=rc.secid order by rcid desc ");
			
			ResultSet resultSet = null;
			resultSet = stmt.executeQuery(sBuffer.toString());
			
			//?????SQL???
			String updateSql = "UPDATE TBLBIZ_RC_TABLE SET XXTGBM=? WHERE RCID=?";
			PreparedStatement pStatement = null;
			pStatement = conn.prepareStatement(updateSql);
			
			//??????
			int count = 0;
			
			//??????TBLBIZ_RC???????TBLBIZ_RC_TABLE?????????????????RC.DEPTNAME?????RT.XXTGBM
			
			while (resultSet.next()) {
				pStatement.setString(1, resultSet.getString(2));
				pStatement.setString(2, resultSet.getString(1));
				int i = 0;
				i = pStatement.executeUpdate();
				System.out.println("i="+i);
				count++;
			}
			
			System.out.println("OK! update"+" "+count+" records successfully!");
			pStatement.close();
			resultSet.close();
			stmt.close();
			
			conn.close();
		} catch (SQLException e) {
			System.out.println("--------------SQL Exception!---------------");
			//e.printStackTrace();
			System.out.println(e.getErrorCode());
			System.out.println(e.getMessage());
		}
		System.out.println("?????????????????д??????У?");
	}

}
